<?php

namespace app\common\utils;

use app\common\model\File;
use Intervention\Image\ImageManagerStatic as Image;

class UploadFile
{
    public static function upload($fileType = ''): array
    {
        $files = request()->file();
        
        $file = current($files);
        
        if (empty($fileType)) {
            $fileType = request()->get('type', 'image');
        }
        try {
            
            if (!$file || !$file->isValid()) {
                throw new \Exception('未找到文件');
            }
            
            // 文件大小
            $fileSize = $file->getSize();
            
            // 文件后缀
            $fileSuffix = strtolower($file->getUploadExtension());
            $uploadSetting = getOption('upload_setting');
            
            $extensions = $uploadSetting[$fileType]['extensions'];
            $maxFileSize = $uploadSetting[$fileType]['upload_max_filesize'];
            
            if ($fileSize > $maxFileSize * 1024) {
                throw new \Exception('当前文件过大，请更换后上传');
            }
            
            if (!in_array($fileSuffix, explode(",", $extensions))) {
                throw new \Exception('当前文件格式不支持');
            }
            
            $filePath = date('Ymd') . '/' . md5(uniqid()) . '.' . $fileSuffix;
            $fileDir = public_path() . '/upload/' . $filePath;
            
            $result = [
                'file_name' => $file->getUploadName(),
                'file_suffix' => $fileSuffix,
                'file_md5' => md5_file($file->getPathName()),
                'file_size' => $fileSize,
                'file_path' => $filePath,
                'file_type' => $fileType,
                'storage' => 1
            ];
            
            $objFile = File::where('file_md5', '=', $result['file_md5'])->find();
            if (empty($objFile)) {
                if (!file_exists(dirname($fileDir))) {
                    mkdir(dirname($fileDir), 0777, true);
                }
                if (in_array($fileSuffix, $uploadSetting['image'])) {
                    $image = Image::make($file);
                    $image->save($fileDir);
                } else {
                    $file->move($fileDir);
                }
                File::create($result);
            } else {
                $result['file_path'] = $objFile['file_path'];
            }
            return [
                'path' => $result['file_path'],
                'url' => getImageUrl($result['file_path']),
                'name' => $file->getUploadName(),
                'create_time' => date('Y-m-d H:i:s')
            ];
        } catch (\Exception $e) {
            return [
                'errMsg' => $e->getMessage()
            ];
        }
        
    }
    
    // 计量格式化
    public static function sizeFormat(int $byte): string
    {
        
        $KB = 1024;
        $MB = 1024 * $KB;
        $GB = 1024 * $MB;
        $TB = 1024 * $GB;
        if ($byte < $KB) {
            return $byte . "B";
        } elseif ($byte < $MB) {
            return round($byte / $KB, 2) . "KB";
        } elseif ($byte < $GB) {
            return round($byte / $MB, 2) . "MB";
        } elseif ($byte < $TB) {
            return round($byte / $GB, 2) . "GB";
        } else {
            return round($byte / $TB, 2) . "TB";
        }
    }
}